<?php

namespace App\Repository;

use App\Entity\PlatIncome;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;

/**
 * @method PlatIncome|null find($id, $lockMode = null, $lockVersion = null)
 * @method PlatIncome|null findOneBy(array $criteria, array $orderBy = null)
 * @method PlatIncome[]    findAll()
 * @method PlatIncome[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class PlatIncomeRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, PlatIncome::class);
    }

    public function getByDay($flag = 0 , $day = ""){
        if (!$day) {
            $day = date(YYMMDD);
        }
        $data = $this->findOneBy(array("flag"=>$flag,"day"=>$day));
        if (!$data) {
            $data = new PlatIncome();
            $data->setFlag($flag)
                ->setDay($day)
                ->setTotal(0)
                ->setCreateAt(new \DateTime());
            $this->_em->persist($data);
            $this->_em->flush();
        }
        return $data;
    }


    // /**
    //  * @return PlatIncome[] Returns an array of PlatIncome objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('p')
            ->andWhere('p.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('p.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?PlatIncome
    {
        return $this->createQueryBuilder('p')
            ->andWhere('p.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}
